﻿using ERPPublic;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.Formula.PTG;
using ProcurementManagement.Applcation.Command.QualityCommand;
using ProcurementManagement.Domain.Dtos.Quality;
using ProcurementManagement.Dtos;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace ProcurementManagement.Controllers
{
    /// <summary>
    /// 质检控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class QualityController : ControllerBase
    {
        IMediator _mediator;


        public QualityController(IMediator mediator)
        {
            _mediator = mediator;
        }


        /// <summary>
        /// 添加质检申请
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddProPcApply(AddProPcApply command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;                                                  // 
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.AddName = name;
                command.AddTime = DateTime.Now;
                //获取用户ID
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                command.User_Id = int.Parse(userId);
                result.Data = await _mediator.Send(new CreateQualityCommand { AddDTo = command});
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }

            return result;
        }


        /// <summary>
        /// 质检申请列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDTO<SelectProPcApplyDTO>>> ShowProPcApply([FromQuery]SelectProPcApplyCommand command)
        {
            ApiResult<PageDTO<SelectProPcApplyDTO>> result = new ApiResult<PageDTO<SelectProPcApplyDTO>>();

            try
            {
                 result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
           return result;
        }



        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public  async Task<ApiResult<int>> DelProPcApply(DelProApplyCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();

            try
            {
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.DelNmae = name;
                result.Data   = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }

            return result;
        }


        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<SelectProPcApplyDTO>> BackFillProApply([FromQuery]FillProApplyCommand command)
        {
            ApiResult<SelectProPcApplyDTO> result = new ApiResult<SelectProPcApplyDTO>();

            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
return result;
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> UptProPcApply(UptProPcApply command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.UpdName = name;
                command.UpdTime = DateTime.Now;
                result.Data = await _mediator.Send(new UptProApplyCommand { UptDTO = command });
                result.Message = "修改成功";
                result.Code = 200;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }

            return result;
        }
    }
}
